#include "read_boy_confg.h"
#include "read_boy_book.h"
#include "ff.h"

typedef struct book_contents{
    char lines;//一页显示多少行
    char page;//共多少页
}book_contents_t;
//显示目录界面
//my_read_boy_book.new_index
void read_boy_book_show_Table_of_Contents(book_contents_t *contents){
    FIL file;  // 文件对象
    FRESULT res;  // FatFs返回结果
    char out_buff[128];
    uint32_t rd_len;   

    char contents_start ; 
    char contents_end ; 
    char contents_num ; 

    contents_num = my_read_boy_book.new_index % contents->lines;
    
    contents_start =  my_read_boy_book.new_index / contents->lines * contents->lines;
//    if((my_read_boy_book.new_index >= contents->lines) && (contents_num == 0)){
//        contents_start += contents->lines;
//    }
    
    
    contents_end = my_read_boy_book.new_index / contents->lines + 1;
//    if(contents_num){
//        contents_end += 1;
//    }
    if(contents_end == contents->page){
        contents_end = my_read_boy_book.sd_book_total_count;
    }else{
        contents_end = contents_start + contents->lines;
    }
    
                                    
    res = f_open(&file,  BOOK_PATH_NEWFILE_TXT, FA_READ);
    if (res == FR_OK) {
        draw_string_for_sd_start( );    
        for(int i = contents_start; i < contents_end; i++){
            res = f_lseek(&file, i*128);
            if (res == FR_OK) {
                res = f_read(&file, out_buff, 128, &rd_len);
                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (i-contents_start), 24,out_buff);

            }
        } 
        draw_string_for_sd_end();    
        
        
    }
    f_close(&file) ;
    read_boy_draw_char_en(my_read_boy_book.iX_initial_value + 10  ,my_read_boy_book.iY_initial_value + contents_num*24,read_boy_FONT_16x24, '>',0x0);
    //read_boy_draw_char_en(10 + CONTENTS_IX,0 + my_read_boy_book.new_index*24,read_boy_FONT_16x24, '>',0);

}

char read_boy_book_show_Contents(){//显示目录界面
    book_contents_t my_book_contents;
    
    char key_num;
    static char flag = 0;
    
    my_book_contents.lines = my_read_boy_book.hight / 24;
    my_book_contents.page  = my_read_boy_book.sd_book_total_count / my_book_contents.lines;
    if(my_read_boy_book.sd_book_total_count % my_book_contents.lines){
        my_book_contents.page  += 1;
    }
    while(1){
        key_num = readboy_key_scan();
        switch(flag){
            case 0:
                READ_BOY_BOOK_DISP_CLEAR; 
                flag = 1;
            //break;
            case 1:
                read_boy_book_show_Table_of_Contents(&my_book_contents);
                
                READ_BOY_BOOK_DISP; 
                flag = 2;
            break;
            case 2:
                if(key_num == 1){
                    my_read_boy_book.old_index = my_read_boy_book.new_index;
                    my_read_boy_book.new_index++;// = (my_read_boy_book.new_index + 1) % 2;
                    my_read_boy_book.new_index = my_read_boy_book.new_index % my_read_boy_book.sd_book_total_count;
                    flag = 0;
                }
                if(key_num == 2){
                    flag = 0;
                    return 2;
                }
            break;
                
        }
        
    }
    
    return 0;
    
}

//#define CONTENTS_IX  300
////显示目录
//void read_boy_book_show_Table_of_Contents2(){
//    FIL file;  // 文件对象
//    FRESULT res;  // FatFs返回结果
//    char out_buff[128];
//    uint32_t rd_len;                                
//                                    
//    res = f_open(&file,  BOOK_PATH_NEWFILE_TXT, FA_READ);
//    if (res == FR_OK) {
//        draw_string_for_sd_start( );    
//        for(int i = 0; i < my_read_boy_book.sd_book_total_count; i++){
//            res = f_lseek(&file, i*128);
//            if (res == FR_OK) {
//                res = f_read(&file, out_buff, 128, &rd_len);
//                draw_string_for_sd(40 + CONTENTS_IX,24 * i, 24,out_buff);

//            }
//        } 
//        draw_string_for_sd_end();    
//        
//        
//    }
//    f_close(&file) ; 

//}
//char read_boy_book_show_Contents2(){//显示目录界面
//    char key_num;
//    static char flag = 0;
//    while(1){
//        key_num = readboy_key_scan();
//        switch(flag){
//            case 0:
//                READ_BOY_BOOK_DISP_CLEAR; 
//                flag = 1;
//            //break;
//            case 1:
//                read_boy_book_show_Table_of_Contents2();
//                read_boy_draw_char_en(10 + CONTENTS_IX,0 + my_read_boy_book.old_index*24,read_boy_FONT_16x24, '>',0xff);
//                read_boy_draw_char_en(10 + CONTENTS_IX,0 + my_read_boy_book.new_index*24,read_boy_FONT_16x24, '>',0);
//                READ_BOY_BOOK_DISP; 
//                flag = 2;
//            break;
//            case 2:
//                if(key_num == 1){
//                    my_read_boy_book.old_index = my_read_boy_book.new_index;
//                    my_read_boy_book.new_index++;// = (my_read_boy_book.new_index + 1) % 2;
//                    my_read_boy_book.new_index = my_read_boy_book.new_index % my_read_boy_book.sd_book_total_count;
//                    flag = 1;
//                }
//                if(key_num == 2){
//                    flag = 0;
//                    return 2;
//                }
//            break;
//                
//        }
//        
//    }
//    
//    return 0;
//    
//}


